/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.GUI;

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.InputManager;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.ui.Picture;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import mygame.Backbone.GlobalVars;
import mygame.Manager;
import mygame.Player.Ressource;

/**
 *
 * @author Mr.N0I3oDy
 */
public class GUIManager implements ScreenController {

    private Nifty nifty;
    private NiftyJmeDisplay niftyDisplay;
    private Node guiNode;
    private BitmapText txt;
    private AssetManager asm;
    private InputManager ipm;
    private AudioRenderer adr;
    private ViewPort vp;
    private MenuHandler meh;
    private boolean inmenu = false;

    public GUIManager(Node guiNode, AssetManager asm, InputManager ipm, AudioRenderer audioRenderer, ViewPort vp) {
        this.asm = asm;
        this.ipm = ipm;
        this.adr = audioRenderer;
        this.vp = vp;
        this.guiNode = guiNode;
        this.meh = new MenuHandler(ipm,this);
    }

    public void init() {
//        Picture pic = new Picture("HUD Picture");
//        pic.setImage(Manager.GetInstance().getAssetManager(), "Textures/Crosshair.png", true);
//        pic.setWidth(40);
//        pic.setHeight(40);
//        pic.setPosition(Manager.GetInstance().getSettings().getWidth() / 2, Manager.GetInstance().getSettings().getHeight() / 2);
//        guiNode.attachChild(pic);
        setText();
    }

    private void setText() {
        BitmapFont f = Manager.GetInstance().getAssetManager().loadFont("Interface/Fonts/BritannicBold.fnt");
        txt = new BitmapText(f, false);
        txt.setSize(f.getCharSet().getRenderedSize());
        txt.setColor(ColorRGBA.White);
        txt.setText("Inventory:");
        txt.setLocalTranslation(10f, Manager.GetInstance().getSettings().getHeight() - 10f, 0);
        guiNode.attachChild(txt);
    }

    public void update() {
        if (!inmenu) {
            String text = "Inventory:\n";
            for (Ressource r : Manager.GetInstance().getPlayer().getRessources()) {
                text += r.getObject() + ": " + r.getCount() + "\n";
            }
            txt.setText(text);
        }
    }

    public void callMenu() {
        setupNifty("Interface/Nifty/Start.xml", "start");
        inmenu = true;
    }

    private void setupNifty(String Path, String StartScreen) {
        guiNode.detachAllChildren();
        niftyDisplay = new NiftyJmeDisplay(asm, ipm, adr, vp);
        nifty = niftyDisplay.getNifty();
        nifty.fromXml(Path, StartScreen, this);
        vp.addProcessor(niftyDisplay);
        ipm.setCursorVisible(true);
        //Manager.GetInstance().setGameMode(GlobalVars.GAME_MODE_MENU);
    }

    public void removeNiftyGUI() {
        this.nifty.exit();
        this.vp.removeProcessor(this.niftyDisplay);
        this.nifty = null;
        this.niftyDisplay.cleanup();
        this.niftyDisplay = null;
        this.guiNode.detachAllChildren(); //entfernt ingame hud

        Manager.GetInstance().setGameMode(GlobalVars.GAME_MODE_PLAYING);
    }

    public void bind(Nifty nifty, Screen screen) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onStartScreen() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onEndScreen() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public MenuHandler getMenuHandler() {
        return this.meh;
    }
    
    public void setInMenu(boolean inmenu){
        this.inmenu = inmenu;
    }
}
